package com.bsoft.messengerclient;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.socks.library.KLog;

public class MainActivity extends AppCompatActivity {

    private static final int FLAG = 0x110;
    private Messenger mService;
    private boolean isConn = false;

    private TextView tv_test;
    private Button btn_test;
    /**
     * 客户端Messenger
     */
    private Messenger messenger = new Messenger(new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case FLAG:
                    KLog.d("客户端收到服务端处理的结果："+msg.arg2);
                    tv_test.setText("20+10="+msg.arg2);
                    break;
            }
        }
    });

    /**
     * 连接服务端 Messenger
     */
    private ServiceConnection mConnect = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mService = new Messenger(service);
            isConn = true;
            KLog.d("链接服务端成功");
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mService = null;
            isConn = false;
            KLog.d("链接服务端失败");
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ServiceInvoked();
        tv_test = (TextView) findViewById(R.id.tv_test);
        btn_test = (Button) findViewById(R.id.btn_test);

        btn_test.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Message msgFromClient = Message.obtain(null, FLAG, 20, 10);
                msgFromClient.replyTo = messenger;
                if (isConn) {
                    //往服务端发送消息
                    try {
                        KLog.d("给服务端发送数据:"+msgFromClient.arg1+"&&"+msgFromClient.arg2);
                        mService.send(msgFromClient);
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }

                }
            }
        });



    }

    private void ServiceInvoked() {
        final Intent intent = new Intent();
        intent.setAction("com.bsoft.messengerserver");
        intent.setPackage("com.bsoft.messengerserver");
        bindService(intent, mConnect, Service.BIND_AUTO_CREATE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(mConnect);
    }
}
